﻿package org.openChart.display 
{
	import flash.geom.Point;
	
	/**
	 * A group of display labels of axis.
	 * 坐标轴刻度栏可能是水平竖直的，当然也可能有一定的斜角。
	 * 如果标题太多或标题太长，可能会相互遮掩，为了避免这种状况，
	 * 设计算法隐藏部分标题。水平或竖直的比较容易，有一定斜角的比较复杂。
	 * 
	 * @author Chongyuan
	 */
	public class DisplayLabelGroup 
	{
		private var textLabels_:Array;
		
		private var maxHeight_:Number = Number.MIN_VALUE;
		private var maxWidth_:Number = Number.MIN_VALUE;
		private var length_:uint = 0;
		
		public function DisplayLabelGroup() 
		{
			textLabels_ = new Array();
		}
		/**
		 * Add a text label to this group.
		 * @param	textLabel
		 */
		public function addTextLabel( textLabel:DisplayTextLabel ):void
		{
			textLabels_.push(textLabel);
			if (maxHeight < textLabel.height)
				maxHeight_ = textLabel.height;
			if (maxWidth < textLabel.width)
				maxWidth_ = textLabel.width;
			length_++;
		}
		
		/**
		 * Remove all the labels from group.
		 */
		public function clear():void
		{
			for (var i:uint = 0; i < length_; i++)
			{
				textLabels_.pop();
			}
			length_ = 0;
			maxHeight_ = Number.MIN_VALUE;
			maxWidth_ = Number.MIN_VALUE;
		}
		
		/**
		 * Get the text label at the position index. 
		 * @param	index
		 * @return
		 */
		public function getAt(index:uint):DisplayTextLabel
		{
			if (index >= length_ || index < 0)
				throw new Error("Index out of boundary");
			var textLabel:DisplayTextLabel = textLabels_[index] as DisplayTextLabel;
			return textLabel;
		}
		
		/**
		 * Set the position of label.
		 * @param	index
		 * @param	position
		 */
		public function setPositionAt(index:uint, position:Point):void
		{
			if (index >= length_ || index < 0)
				throw new Error("Index out of boundary");
			var textLabel:DisplayTextLabel = textLabels_[index] as DisplayTextLabel;
			textLabel.position = position;
		}
		
		/**
		 * If there are too many labels to show, hide some of them.
		 */
		public function hideLabels():void
		{
			//TODO: implement the algorithm to hide labels. 
		}
		
		public function get maxHeight():Number
		{
			return this.maxHeight_;
		}
		
		public function get maxWidth():Number
		{
			return this.maxWidth_;
		}
		
		public function get length():Number
		{
			return length_;
		}
		
		public function toString():String
		{
			var str:String = "\nObject:DisplayLabelGroup";
			str += "[maxWidth = " + maxWidth + "]";
			str += "[maxHeight = " + maxHeight + "]";
			str += "[length = " + length + "]";
			for (var i:uint = 0; i < length; i++ )
			{
				str += getAt(i);
			}
			return str;
		}
	}
	
}